package com.webserver.controller;

import com.webserver.annotation.Controller;
import com.webserver.annotation.RequestMapping;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * 保存所有请求与对应的业务处理关系
 */
public class HandlerMapping {
    /*
        key:请求路径
        value：处理该请求的业务及对应方法

        例如：
        key：/myweb/reg
        value：MethodMapping{
            controller:UserController
            method:reg();
        }
     */
    private static Map<String, MethodMapping> mapping = new HashMap<>();

    public static MethodMapping getMethodMapping(String path){
        return mapping.get(path);
    }


    public static void setMapping(Map<String, MethodMapping> mapping) {
        HandlerMapping.mapping = mapping;
    }

    static {
            initMapping();

    }

    private static void initMapping(){
        /*
            扫描com.webserver.controller包中的所有类，并将所有被注解@Controller
            的类实例化，然后将@RequestMapping标注的方法获取，并将这两项一个
            MethodMapping表示，最终作为@RequestMapping注解传递的参数(请求路径)作为
            key，将这个MethodMapping作为value保存到mapping这个map

         */
        try {
            File   dir = new File(
                        HandlerMapping.class.getClassLoader().getResource(
                                "./com/webserver/controller/").toURI()
                );
            File[] subs = dir.listFiles(f -> f.getName().endsWith(".class"));
            System.out.println(subs.length);
            for (File sub : subs) {
                String fileName = sub.getName();
                String className = fileName.substring(0, fileName.indexOf("."));
                Class cls = Class.forName("com.webserver.controller." + className);
                if (cls.isAnnotationPresent(Controller.class)) {
                    Object o = cls.newInstance();
                    System.out.println(className + "被@AutoRunClass标注了！");
                    Method[] methods = cls.getDeclaredMethods();
                    for (Method method : methods) {
                        if (method.isAnnotationPresent(RequestMapping.class)) {
                            System.out.println(method.getName() + "被@AutoRunMethod标注了");
                            RequestMapping rm = method.getAnnotation(RequestMapping.class);
                            String path = rm.value();
                            MethodMapping methodMapping = new MethodMapping(o,method);
                            mapping.put(path, methodMapping);
                        }
                    }
                }
            }
        } catch (URISyntaxException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        System.out.println(mapping.size());




    }

    public static void main(String[] args) {
    }

    public static class MethodMapping {
        //某个Controller实例
        private Object controller;
        //对应的处理业务方法
        private Method method;

        public MethodMapping(Object controller, Method method) {
            this.controller = controller;
            this.method = method;
        }

        public Object getController() {
            return controller;
        }

        public void setController(Object controller) {
            this.controller = controller;
        }

        public Method getMethod() {
            return method;
        }

        public void setMethod(Method method) {
            this.method = method;
        }

    }

    //
//    private Object obj;
//    private Method method;
//
//    public HandlerMapping(Object obj, Method method) {
//        this.obj = obj;
//        this.method = method;
//    }
//
//    public Object getObj() {
//        return obj;
//    }
//
//    public Method getMethod() {
//        return method;
//    }
}

